using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Globalization;
using System.Collections;
// Both Resources & Reflection're needed for getting strings from resources.
using System.Resources;
using System.Reflection;



namespace UnitConverterSmartphone
{
    /// <summary>
    /// Summary description for form.
    /// </summary>
    public class MainForm : System.Windows.Forms.Form
    {
        ArrayList m_CategoryList = new ArrayList();
        private Label labelAbbrOut;
        private Label labelAbbrInp;
        private ComboBox comboBoxOut;
        private ComboBox comboBoxInp;
        private TextBox textBoxInp;
        private ComboBox comboBoxCategory;
        private MenuItem menuItemExit;
        private MenuItem menuItemInfo;
        private Label textBoxOut;
        private MenuItem menuItemInput;
        private MenuItem menuItemOutput;
        private MenuItem menuItemAbout;
        /// <summary>
        /// Main menu for the form.
        /// </summary>
        private System.Windows.Forms.MainMenu mainMenu1;

        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.menuItemExit = new System.Windows.Forms.MenuItem();
            this.menuItemInfo = new System.Windows.Forms.MenuItem();
            this.labelAbbrOut = new System.Windows.Forms.Label();
            this.labelAbbrInp = new System.Windows.Forms.Label();
            this.comboBoxOut = new System.Windows.Forms.ComboBox();
            this.comboBoxInp = new System.Windows.Forms.ComboBox();
            this.textBoxInp = new System.Windows.Forms.TextBox();
            this.comboBoxCategory = new System.Windows.Forms.ComboBox();
            this.textBoxOut = new System.Windows.Forms.Label();
            this.menuItemInput = new System.Windows.Forms.MenuItem();
            this.menuItemOutput = new System.Windows.Forms.MenuItem();
            this.menuItemAbout = new System.Windows.Forms.MenuItem();
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.Add(this.menuItemExit);
            this.mainMenu1.MenuItems.Add(this.menuItemInfo);
            // 
            // menuItemExit
            // 
            this.menuItemExit.Text = "Exit";
            this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);
            // 
            // menuItemInfo
            // 
            this.menuItemInfo.MenuItems.Add(this.menuItemInput);
            this.menuItemInfo.MenuItems.Add(this.menuItemOutput);
            this.menuItemInfo.MenuItems.Add(this.menuItemAbout);
            this.menuItemInfo.Text = "Info";
            this.menuItemInfo.Click += new System.EventHandler(this.menuItemExit_Click);
            // 
            // labelAbbrOut
            // 
            this.labelAbbrOut.Location = new System.Drawing.Point(116, 113);
            this.labelAbbrOut.Size = new System.Drawing.Size(57, 21);
            this.labelAbbrOut.Text = "label";
            // 
            // labelAbbrInp
            // 
            this.labelAbbrInp.Location = new System.Drawing.Point(116, 47);
            this.labelAbbrInp.Size = new System.Drawing.Size(57, 21);
            this.labelAbbrInp.Text = "label";
            // 
            // comboBoxOut
            // 
            this.comboBoxOut.Location = new System.Drawing.Point(4, 140);
            this.comboBoxOut.Size = new System.Drawing.Size(169, 22);
            this.comboBoxOut.SelectedValueChanged += new System.EventHandler(this.comboBoxOut_SelectedIndexChanged);
            // 
            // comboBoxInp
            // 
            this.comboBoxInp.Location = new System.Drawing.Point(4, 76);
            this.comboBoxInp.Size = new System.Drawing.Size(169, 22);
            this.comboBoxInp.SelectedValueChanged += new System.EventHandler(this.comboBoxInp_SelectedIndexChanged);
            // 
            // textBoxInp
            // 
            this.textBoxInp.Location = new System.Drawing.Point(4, 47);
            this.textBoxInp.Size = new System.Drawing.Size(106, 24);
            this.textBoxInp.Text = "textBoxInp";
            this.textBoxInp.TextChanged += new System.EventHandler(this.textBoxInpOut_TextChanged);
            // 
            // comboBoxCategory
            // 
            this.comboBoxCategory.Location = new System.Drawing.Point(4, 3);
            this.comboBoxCategory.Size = new System.Drawing.Size(169, 22);
            this.comboBoxCategory.SelectedValueChanged += new System.EventHandler(this.comboBoxCategory_SelectedIndexChanged);
            // 
            // textBoxOut
            // 
            this.textBoxOut.Font = new System.Drawing.Font("Nina", 11F, System.Drawing.FontStyle.Regular);
            this.textBoxOut.Location = new System.Drawing.Point(4, 113);
            this.textBoxOut.Size = new System.Drawing.Size(106, 18);
            this.textBoxOut.Text = "textBoxOut";
            // 
            // menuItemInput
            // 
            this.menuItemInput.Text = "input";
            this.menuItemInput.Click += new System.EventHandler(this.menuItemInput_Click);
            // 
            // menuItemOutput
            // 
            this.menuItemOutput.Text = "output";
            this.menuItemOutput.Click += new System.EventHandler(this.menuItemOutput_Click);
            // 
            // menuItemAbout
            // 
            this.menuItemAbout.Text = "About";
            this.menuItemAbout.Click += new System.EventHandler(this.menuItemAbout_Click);
            // 
            // MainForm
            // 
            this.ClientSize = new System.Drawing.Size(176, 180);
            this.Controls.Add(this.comboBoxCategory);
            this.Controls.Add(this.textBoxInp);
            this.Controls.Add(this.comboBoxInp);
            this.Controls.Add(this.comboBoxOut);
            this.Controls.Add(this.labelAbbrInp);
            this.Controls.Add(this.labelAbbrOut);
            this.Controls.Add(this.textBoxOut);
            this.Menu = this.mainMenu1;
            this.Text = "Unit Converter";
            this.Load += new System.EventHandler(this.MainForm_Load);

        }

        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            Application.Run(new MainForm());
        }

        private void numericUpDownDecPlaces_ValueChanged(object sender, EventArgs e)
        {

        }

        private void buttonOutInfo_Click(object sender, EventArgs e)
        {

        }

        private void buttonInpInfo_Click(object sender, EventArgs e)
        {

        }

        private void comboBoxOut_SelectedIndexChanged(object sender, EventArgs e)
        {
            Unit outUnit = (Unit)(comboBoxOut.SelectedItem);
            labelAbbrOut.Text = outUnit.abbr;

            menuItemOutput.Text = outUnit.name;

            menuItemOutput.Enabled = outUnit.info.Length > 0;

            //buttonOutInfo.Enabled = (outUnit.info.Length > 0);

            ReCalculate();
        }

        private void comboBoxInp_SelectedIndexChanged(object sender, EventArgs e)
        {
            Unit inpUnit = (Unit)(comboBoxInp.SelectedItem);
            labelAbbrInp.Text = inpUnit.abbr;

            menuItemInput.Text = inpUnit.name;

            menuItemInput.Enabled = inpUnit.info.Length > 0;

            //buttonInpInfo.Enabled = (inpUnit.info.Length > 0);

            ReCalculate();
        }

        private void textBoxInpOut_TextChanged(object sender, EventArgs e)
        {
            ReCalculate();
        }

        private void comboBoxCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            Category currCategory = (Category)(comboBoxCategory.SelectedItem);

            comboBoxInp.Items.Clear();
            comboBoxOut.Items.Clear();

            for (int i = 0; i < currCategory.units.Count; i++)
            {
                comboBoxInp.Items.Add(currCategory.units[i]);
                comboBoxOut.Items.Add(currCategory.units[i]);
            }

            comboBoxInp.SelectedIndex = 0;
            comboBoxOut.SelectedIndex = 0;
        }


        private void ReCalculate()
        {
            Color outBoxRightColor = SystemColors.Window;
            Color outBoxWrongColor = Color.Coral;

            Category currCategory = (Category)(comboBoxCategory.SelectedItem);

            Unit inpUnit = (Unit)(comboBoxInp.SelectedItem);
            Unit outUnit = (Unit)(comboBoxOut.SelectedItem);

            if ((inpUnit == null) || (outUnit == null)) return;

            try
            {
                double dValue = Double.Parse(textBoxInp.Text, CultureInfo.InvariantCulture);

                double dConvertedValue = ((dValue * inpUnit.lf + inpUnit.cf) - outUnit.cf) / outUnit.lf;

                textBoxOut.Text = dConvertedValue.ToString("G"/* + numericUpDownDecPlaces.Value*/);

                textBoxOut.BackColor = outBoxRightColor;
            }
            catch
            {
                textBoxOut.Text = "Invalid number";
                textBoxOut.BackColor = outBoxWrongColor;
            }
        }


        void LoadXML(XmlNode root)
        {
            if (!root.HasChildNodes) return;

            foreach (XmlNode categoryNode in root.ChildNodes)
            {
                Category newCategory = new Category(categoryNode.Attributes["name"].Value);

                foreach (XmlNode UnitNode in categoryNode.ChildNodes)
                {
                    XmlAttributeCollection attr = UnitNode.Attributes;

                    Unit unit = new Unit();

                    //Read unit name, linear factor, constant factor
                    unit.name = attr["name"].Value;
                    unit.abbr = attr["abbr"].Value;

                    if (attr["info"] != null)
                        unit.info = attr["info"].Value;
                    else
                        unit.info = "";

                    if (attr["cf"] != null)
                    {
                        unit.cf = Double.Parse(attr["cf"].Value, CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        unit.cf = 0;
                    }

                    unit.lf = Double.Parse(attr["lf"].Value, CultureInfo.InvariantCulture);

                    newCategory.units.Add(unit);
                }

                m_CategoryList.Add(newCategory);

            }

        }


        void ReadXMLData()
        {
            //XmlTextReader Xreader = new XmlTextReader("ConvertData.xml");
            //XmlDocument XmlDoc = new XmlDocument();
            //XmlDoc.Load(Xreader);
            
            //Reading XML Document with MAIN units.
            //ResourceManager ResManager = new ResourceManager("UnitConverterSmartphone.Properties.Resources", typeof(Properties.Resources).Assembly);
            ResourceManager ResManager = new ResourceManager("UnitConverterSmartphone.ManualRes", Assembly.GetExecutingAssembly());
            String xmltext = ResManager.GetString("ConvertData");
            XmlDocument XmlDoc = new XmlDocument();
            XmlDoc.LoadXml(xmltext);
            
            LoadXML(XmlDoc.DocumentElement);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            ReadXMLData();

            foreach (Category currCategory in m_CategoryList)
            {
                comboBoxCategory.Items.Add(currCategory);
            }

            comboBoxCategory.SelectedIndex = 0;
            comboBoxCategory.Focus();

            textBoxInp.Text = "1";
            textBoxOut.Text = "1";

            //numericUpDownDecPlaces.Minimum = 1;
            //numericUpDownDecPlaces.Maximum = 20;
            //numericUpDownDecPlaces.Value = 10;
        }


        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Unit Converter for smartphones\nRustam Iskenderov, http://code.google.com/p/unit-converter-for-smartphones");
        }

        private void menuItemInfo_Click(object sender, EventArgs e)
        {
            String text = "";
            Unit Unit = (Unit)(comboBoxInp.SelectedItem);

            text += " " + Unit.name + "\n" + Unit.info + "\n\n";

            Unit = (Unit)(comboBoxOut.SelectedItem);

            text += " " + Unit.name + "\n" + Unit.info + "\n\n";

            MessageBox.Show(text);
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menuItemOutput_Click(object sender, EventArgs e)
        {
            Unit Unit = (Unit)(comboBoxOut.SelectedItem);
            MessageBox.Show(Unit.info, Unit.name);
        }

        private void menuItemInput_Click(object sender, EventArgs e)
        {
            Unit Unit = (Unit)(comboBoxInp.SelectedItem);
            MessageBox.Show(Unit.info, Unit.name);
        }



    }
}

